Conditions | 32 |
Total Lines | 126 |
Code Lines | 92 |
Lines | 126 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like codef_scrolltext.js ➔ scrolltext_vertical often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*------------------------------------------------------------------------------ |
||
176 | View Code Duplication | function scrolltext_vertical(){ |
|
177 | this.scroffset=0; |
||
178 | this.oldspeed=0; |
||
179 | this.speed=1; |
||
180 | this.font; |
||
181 | this.letters = new Object(); |
||
182 | this.scrtxt=" "; |
||
183 | this.pausetimer=0; |
||
184 | this.pausedelay=0; |
||
185 | |||
186 | this.init = function(dst, font,speed,sinparam,type){ |
||
187 | this.speed=speed; |
||
188 | this.dst=dst; |
||
189 | this.font=font; |
||
190 | this.fontw = this.font.tilew; |
||
191 | this.fonth = this.font.tileh; |
||
192 | this.fontstart = this.font.tilestart; |
||
193 | this.wide=Math.ceil(this.dst.canvas.height/this.fonth)+1; |
||
194 | for(i=0;i<=this.wide;i++){ |
||
195 | this.letters[i]=new ltrobj(0,Math.ceil((this.wide*this.fonth)+i*this.fonth),this.scrtxt.charCodeAt(this.scroffset)); |
||
196 | this.scroffset++; |
||
197 | } |
||
198 | if(typeof(sinparam)!='undefined') |
||
199 | this.sinparam=sinparam; |
||
200 | if(typeof(type)=='undefined') |
||
201 | this.type=0; |
||
202 | else |
||
203 | this.type=type; |
||
204 | } |
||
205 | |||
206 | this.draw = function(posx){ |
||
207 | var prov = 0; |
||
208 | var temp = new Array(); |
||
209 | var tmp=this.dst.contex.globalAlpha; |
||
210 | this.dst.contex.globalAlpha=1; |
||
211 | var oldvalue=new Array(); |
||
212 | var i; |
||
213 | if(typeof(this.sinparam)!='undefined'){ |
||
214 | for(var j=0;j<this.sinparam.length;j++){ |
||
215 | oldvalue[j]=this.sinparam[j].myvalue; |
||
216 | } |
||
217 | } |
||
218 | if(this.speed==0){ |
||
219 | this.pausetimer+=1; |
||
220 | if(this.pausetimer==60*this.pausedelay){ |
||
221 | this.speed=this.oldspeed; |
||
222 | } |
||
223 | } |
||
224 | var speed=this.speed; |
||
225 | for(i=0;i<=this.wide;i++){ |
||
226 | this.letters[i].posy-=speed; |
||
227 | if(this.letters[i].posy<=-this.fonth){ |
||
228 | if(this.scrtxt.charAt(this.scroffset) =="^"){ |
||
229 | if(this.scrtxt.charAt(this.scroffset+1) =="P"){ |
||
230 | this.pausedelay=this.scrtxt.charAt(this.scroffset+2); |
||
231 | this.pausetimer=0; |
||
232 | this.oldspeed=this.speed; |
||
233 | this.speed=0; |
||
234 | this.scroffset+=3; |
||
235 | } |
||
236 | else if(this.scrtxt.charAt(this.scroffset+1) =="S"){ |
||
237 | this.speed=this.scrtxt.charAt(this.scroffset+2); |
||
238 | this.scroffset+=3; |
||
239 | } |
||
240 | // |
||
241 | // ADDON by Robert Annett |
||
242 | // |
||
243 | else if(this.scrtxt.charAt(this.scroffset+1) =="C"){ |
||
244 | var end = this.scrtxt.indexOf(';', this.scroffset+2); |
||
245 | var functionName = this.scrtxt.substring(this.scroffset+2, end); |
||
246 | window[functionName](); |
||
247 | this.scroffset+=(end-this.scroffset)+1; |
||
248 | } |
||
249 | }else{ |
||
250 | this.letters[i].posy=this.wide*this.fonth+(this.letters[i].posy+this.fonth); |
||
251 | if(typeof(this.sinparam)!='undefined'){ |
||
252 | for(var j=0;j<this.sinparam.length;j++){ |
||
253 | oldvalue[j]+=this.sinparam[j].inc; |
||
254 | } |
||
255 | } |
||
256 | this.letters[i].ltr=this.scrtxt.charCodeAt(this.scroffset); |
||
257 | this.scroffset++; |
||
258 | if(this.scroffset> this.scrtxt.length-1) |
||
259 | this.scroffset=0; |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | if(typeof(this.sinparam)!='undefined'){ |
||
264 | for(var j=0;j<this.sinparam.length;j++){ |
||
265 | this.sinparam[j].myvalue=oldvalue[j]; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | for(j=0;j<=this.wide;j++){ |
||
270 | temp[j]={indice:j, posy:this.letters[j].posy}; |
||
271 | } |
||
272 | temp.sort(sortPosy); |
||
273 | for(i=0;i<=this.wide;i++){ |
||
274 | if(typeof(this.sinparam)!='undefined'){ |
||
275 | prov = 0; |
||
276 | for(var j=0;j<this.sinparam.length;j++){ |
||
277 | if(this.type==0) |
||
278 | prov += Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp; |
||
279 | if(this.type==1) |
||
280 | prov += -Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
281 | if(this.type==2) |
||
282 | prov += Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
283 | } |
||
284 | } |
||
285 | this.font.drawTile(this.dst,this.letters[temp[i].indice].ltr-this.fontstart,prov+posx,this.letters[temp[i].indice].posy); |
||
286 | |||
287 | if(typeof(this.sinparam)!='undefined'){ |
||
288 | for(var j=0;j<this.sinparam.length;j++){ |
||
289 | this.sinparam[j].myvalue+=this.sinparam[j].inc; |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | if(typeof(this.sinparam)!='undefined'){ |
||
294 | for(var j=0;j<this.sinparam.length;j++){ |
||
295 | this.sinparam[j].myvalue=oldvalue[j]+this.sinparam[j].offset; |
||
296 | } |
||
297 | } |
||
298 | this.dst.contex.globalAlpha=tmp; |
||
299 | } |
||
300 | return this; |
||
301 | } |
||
302 | |||
303 |